clear all
set more off
pause on
set trace off
cap log close
set matsize 5000

loc dir "/Users/mateo/Desktop/"
	
	cd "/Users/mateo/Desktop/"
	
		use "/Users/mateo/Desktop/master_violence_landtitle_caracteristicas.dta", clear

merge 1:1 codmpio Año using "/Users/mateo/Desktop/palm.dta"

drop if _merge==2

drop _merge

gen post=(Año>1995)
	gen any_titleXpost=any_title*post
	gen any_title2=any_title
	gen post2=post
	egen total_violence=rowtotal( sumattacks_public sumattacks_guerrilla sumattacks_paramilitary)
	replace total_violence=. if ( sumattacks_public==. & sumattacks_guerrilla==. & sumattacks_paramilitary==.)
	
	** Controls X post
	loc controlsXpost
	loc i
	
	foreach control of loc controls {
	
		loc i c.`control'##post
		
		loc controlsXpost `controlsXpost' `i' 

	}
	
	** Controls X year
	loc controlsXyear
	loc x
	
	foreach control of loc controls {
	
		loc x c.`control'##c.Año
		
		loc controlsXyear `controlsXyear' `x' 

	}
	
	
	di in red "`controlsXyear'"
		
	foreach x in 1995 {
	
		preserve
		
			keep if Año<=`x'
						
			collapse (mean)  spatlagguerrsum100_100, by(codmpio)
			
			ren (spatlagguerrsum100_100)=_`x'
			
			tempfile neighbor
			save `neighbor', replace
		
		
		restore
		
		merge m:1 codmpio using `neighbor'
		drop _m
	
	
	}
	
	
	
* Results African Palm (mechanism 1)			

foreach y of varlist total_violence sumattacks_public sumattacks_paramilitary sumattacks_guerrilla {
					
				cap gen as_`y'=asinh(`y')		
				
				}
	
		
*
eststo clear
set more off

areg  as_total_violence  any_title##c.palm05##i.post2 i.Año c.Año##i.coddepto  `controlsXpost', r cluster(codmpio) absorb(codmpio)
estadd local ALLFE = "$\checkmark$"
estadd local TT = "$\checkmark$"
estadd local MT = "$\checkmark$"
estadd local BC = "$\checkmark$"
summ as_total_violence if e(sample) ==1
estadd local meanDep = trim("`: display %12.4fc `r(mean)''")
estadd local SDDep = trim("`: display %12.4fc `r(sd)''")
eststo

areg  as_sumattacks_paramilitary  any_title##c.palm05##i.post2 i.Año c.Año##i.coddepto  `controlsXpost', r cluster(codmpio) absorb(codmpio)
estadd local ALLFE = "$\checkmark$"
estadd local TT = "$\checkmark$"
estadd local MT = "$\checkmark$"
estadd local BC = "$\checkmark$"
summ as_sumattacks_paramilitary if e(sample) ==1
estadd local meanDep = trim("`: display %12.4fc `r(mean)''")
estadd local SDDep = trim("`: display %12.4fc `r(sd)''")
eststo

areg  as_sumattacks_guerrilla  any_title##c.palm05##i.post2 i.Año c.Año##i.coddepto  `controlsXpost', r cluster(codmpio) absorb(codmpio)
estadd local ALLFE = "$\checkmark$"
estadd local TT = "$\checkmark$"
estadd local MT = "$\checkmark$"
estadd local BC = "$\checkmark$"
summ as_sumattacks_guerrilla if e(sample) ==1
estadd local meanDep = trim("`: display %12.4fc `r(mean)''")
estadd local SDDep = trim("`: display %12.4fc `r(sd)''")
eststo

areg  as_sumattacks_public  any_title##c.palm05##i.post2 i.Año c.Año##i.coddepto  `controlsXpost', r cluster(codmpio) absorb(codmpio)
estadd local ALLFE = "$\checkmark$"
estadd local TT = "$\checkmark$"
estadd local MT = "$\checkmark$"
estadd local BC = "$\checkmark$"
summ as_sumattacks_public if e(sample) ==1
estadd local meanDep = trim("`: display %12.4fc `r(mean)''")
estadd local SDDep = trim("`: display %12.4fc `r(sd)''")
eststo

esttab est*, se ///
	   star(* 0.10 ** 0.05 *** 0.01 ) ///
	   stats(meanDep SDDep N ALLFE TT MT BC, fmt(%12.4fc %12.4fc  %12.0fc %12.0fc %12.0fc %12.0fc %12.0fc %12.0fc) ///
	 label("\small Mean of Outcome" "\small S.D of Outcome" "\small Observations" "\small Municipality and Year FEs" "\small Time Trends" "\small Region $\times$  Post" "\small Controls $\times$ Post" ///
			"\small Individual Covariates" )) ///
	    keep(1.any_title#1.post2 1.post2#c.palm05 1.any_title#1.post2#c.palm05) ///
	   varlabels(as_total_violence "Total Violence" 1.any_title#1.post2 "Any Title $\times$ Post" 1.post2#c.palm05 "Oil Palm $\times$ Post" 1.any_title#1.post2#c.palm05 "Any Title $\times$ Post $\times$ Oil Palm") ///
	   ti("Oil Palm Results") ///	 
	   replace
	   
